/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.security;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.search.SearchTerm;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * 1.
 * Mailer.get(props).from("from").to("to").text('text').send().close();
 * 2.
 * Mailer mailer = Mailer.get(props);
 * mailer.from("va").to("haha").send();
 * mailer.from("xx").to("haha").send();
 * mailer.close();
 * 3.
 * Mailer mailer = Mailer.get(props);
 * Mail mail = mailer.from("xx");
 * mailer.send(mail);
 *
 * @author vacoor
 */
public class Mailer {

    class Mail {
        public Mail to(String to) {
            return this;
        }

        public Mailer send() {
            return Mailer.this.send(this);
        }
    }

    static Mailer get(Properties props) {
        return null;
    }


    Mail from(String from) {
        return new Mail();
    }


    Mailer send(Mail mail) {
        /*
        if (!isConnected) {
            connected
        }
        send
        */
        return this;
    }

    public void close() {

    }


    public static void receive(Properties props) throws MessagingException, IOException {
        Session session = Session.getInstance(props);
        String protocal = "pop3";   // pop3
        Store store = session.getStore(protocal);
        store.connect(props.getProperty("mail.pop3.user"), props.getProperty("mail.pop3.password"));
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        Message[] msg = inbox.search(new SearchTerm() {
            @Override
            public boolean match(Message msg) {
                try {
                    /* 未读*/
                    if (!msg.isSet(Flags.Flag.SEEN)) {
                        return true;
                    }
                    // 最近
                    /*
                    if (msg.isSet(Flags.Flag.RECENT)) {
                        return true;
                    }
                    */
                } catch (MessagingException e) {
                }
                return false;
            }
        });
        for (Message message : msg) {
            // System.out.println(message.getContent());
            message.setFlag(Flags.Flag.SEEN, true);
        }

        System.out.println(msg.length);

        inbox.close(true);
        store.close();
    }

    public void send(Properties props) throws MessagingException, UnsupportedEncodingException {
        Session session = Session.getInstance(props);
        String protocol = null;
        protocol = null != protocol ? protocol : session.getProperty("mail.transport.protocol");
        protocol = null != protocol ? protocol : "smtp";

        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.126.com", 25, "ychwork@126.com", "xltaifsgen");

        MimeMessage msg = new MimeMessage(session);
        msg.setSubject("sub");
        msg.setText("hehe");
        msg.setFrom(new InternetAddress("ychwork@126.com", "Ych"));
        transport.sendMessage(msg, new Address[]{new InternetAddress("ychwork@126.com", "Ych")});

        /*
        transport.addTransportListener(new TransportAdapter() {
            @Override
            public void messageDelivered(TransportEvent e) {
                System.out.println("delivered");
                super.messageDelivered(e);
            }

            @Override
            public void messageNotDelivered(TransportEvent e) {
                System.out.println("not delivered");
                super.messageNotDelivered(e);
            }

            @Override
            public void messagePartiallyDelivered(TransportEvent e) {
                System.out.println("partiallydevliered");
                super.messagePartiallyDelivered(e);
            }
        });
        */
        transport.close();
    }


    public static void main(String[] args) throws MessagingException, IOException {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.host", "smtp.126.com");
        props.setProperty("mail.smtp.user", "ychwork@126.com");
        props.setProperty("mail.smtp.password", "xltaifsgen");

        props.setProperty("mail.pop3.auth", "true");
        props.setProperty("mail.pop3.host", "pop.126.com");
        props.setProperty("mail.pop3.user", "ychwork@126.com");
        props.setProperty("mail.pop3.password", "xltaifsgen");

        props.setProperty("mail.imap.auth", "true");
        props.setProperty("mail.imap.host", "imap.126.com");
        //
        props.setProperty("mail.imap.starttls.enable", "true");

        Session session = Session.getInstance(props);
        URLName urln = new URLName("pop3", "pop3.126.com", 110, null, "ychwork@126.com", "xltaifsgen");

        // Mailer mailer = Mailer.create().from('').cc('xx').cc('xx').bcc('xx');
        // mailer.send('host', '', props);
        // mailer.revei
    }
}
